﻿using System.Windows;
using Ping.App;

namespace Ping.Client.Controls
{
	public partial class SettingsControl
	{
		private Settings _settings;

		public SettingsControl()
		{
			InitializeComponent();
			Loaded += ControlLoaded;
		}

		public event RoutedEventHandler Accepted;
		public event RoutedEventHandler Rejected;

		private void ControlLoaded(object sender, RoutedEventArgs e)
		{
			_settings = SettingsService.Settings;
			appService.Text = _settings.AppServer;
		}

		private void AcceptClick(object sender, RoutedEventArgs e)
		{
			_settings.AppServer = appService.Text;

			SettingsService.Settings = _settings;
			// Storing the settings right-away is a way to prevent
			// losing the changed settings in case the app crashes (before
			// the closing / deactivated event fires).
			SettingsService.Store();

			if (Accepted != null)
				Accepted(sender, e);
		}

		private void RejectClick(object sender, RoutedEventArgs e)
		{
			if (Rejected != null)
				Rejected(sender, e);
		}
	}
}